//
//  RandomViewController.m
//  Menu
//
//  Created by dllo on 15/7/10.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "RandomViewController.h"

@interface RandomViewController ()

@end

@implementation RandomViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isBegin = NO;
    self.isPush = YES;
    self.foodArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor clearColor];
    UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    bgImgView.image = [UIImage imageNamed:@"eatingWhat.png"];
    [self.view addSubview:bgImgView];
    [bgImgView release];
    UIImageView *daLiImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 110*HEIGHT+200*HEIGHT, MyWidth, MyHeight - 64-110*HEIGHT-200*HEIGHT)];
    daLiImageView.image = [UIImage imageNamed:@"dalishuishou.png"];
    [self.view addSubview:daLiImageView];
    [daLiImageView release];
    UILabel *tiShiLabel = [[UILabel alloc] initWithFrame:CGRectMake(250*WIDTH, 405*HEIGHT, 100*WIDTH, 100*HEIGHT)];
    tiShiLabel.text = @"点击下方按钮选择美食吧!";
    tiShiLabel.numberOfLines = 0;
    tiShiLabel.font = [UIFont systemFontOfSize:17*WIDTH];
    [self.view addSubview:tiShiLabel];
    [tiShiLabel release];
    
    self.navigationItem.title = @"翻滚吧美食";
    _scroll = [[MyScrollView alloc]initWithFrame:CGRectMake(20*WIDTH, 64+110*HEIGHT, MyWidth - 40*WIDTH, 200*HEIGHT)];
    _scroll.backgroundColor = [UIColor clearColor];
    UIImageView *place = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zhanwei.png"]];
    place.frame = CGRectMake(20*WIDTH, 64+110*HEIGHT, MyWidth - 40*WIDTH, 200*HEIGHT);
//    [self.view addSubview:place];
    [self.view addSubview:_scroll];
    _scroll.myDelegate = self;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.scroll animated:YES];
    self.HUD.labelText = @"loading";
    [self.HUD show:YES];
    [self loadData];
    
    UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*WIDTH, 64 + 20*HEIGHT, 335*WIDTH, 80*HEIGHT)];
    topLabel.text = @"别再为思考吃什么而浪费时间了\n  动动你的手指 挑选你的美食~";
    topLabel.textAlignment = NSTextAlignmentCenter;
    topLabel.font = [UIFont systemFontOfSize:20];
    topLabel.textColor = [UIColor darkGrayColor];
    topLabel.numberOfLines = 0;
    [self.view addSubview:topLabel];
    [topLabel release];
    
    UIButton *beginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    beginButton.frame = CGRectMake(10*WIDTH, 64+100*HEIGHT+200*HEIGHT+150*HEIGHT, 120*WIDTH, 40*HEIGHT);
    [beginButton setTitle:@"吃什么呢?" forState:UIControlStateNormal];
    beginButton.titleLabel.font = [UIFont systemFontOfSize:20];
    beginButton.layer.cornerRadius = 5;
    beginButton.layer.borderWidth = 3;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 1, 1, 1 });
    [beginButton.layer setBorderColor:colorref];
    
    [beginButton setBackgroundColor:[UIColor colorWithRed:228/255.0 green:213/255.0 blue:189/255.0 alpha:1]];
    [beginButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [beginButton addTarget:self action:@selector(beginButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:beginButton];
    UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeSystem];
    stopButton.frame = CGRectMake(MyWidth-80*WIDTH-30*WIDTH, 64+100*HEIGHT+200*HEIGHT+150*HEIGHT, 100*WIDTH, 40*HEIGHT);
    [stopButton setTitle:@"就它了!" forState:UIControlStateNormal];
    stopButton.titleLabel.font = [UIFont systemFontOfSize:20];
    stopButton.layer.cornerRadius = 5;
    stopButton.layer.borderWidth = 3;
    [stopButton.layer setBorderColor:colorref];
    [stopButton setBackgroundColor:[UIColor colorWithRed:228/255.0 green:213/255.0 blue:189/255.0 alpha:1]];
    [stopButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [stopButton addTarget:self action:@selector(stopButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stopButton];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)] autorelease];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
}
-(void)loadData
{
    for (NSInteger i = 1; i < 6 ; i++) {
        NSString *url = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/found/tblBoutique!getTblBoutiqueVegetableList.do?is_traditional=0&page=%ld&pageRecord=10&phonetype=0&user_id=0&typeId=5",i];

        [AFNTool get:url Body:nil HttpResult:JSON success:^(id result) {
            NSMutableArray *array = [result objectForKey:@"data"];
            for (NSMutableDictionary *dic in array) {
                GoodListModel *goodList = [[GoodListModel alloc] initWithDic:dic];
                [self.foodArray addObject:goodList];
                
            }
            if (i == 5) {
                NSLog(@"%ld",self.foodArray.count);
                self.isBegin = YES;
                [self.scroll setImages:self.foodArray];
                [self.HUD hide:YES];
            }
            
            
        } failure:^(NSError *error) {
            if (i == 5) {
                
            
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前网络不给力" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alertView show];
                [self.HUD hide:YES];

            }
        }];
            
    }
    
    

}
-(void)beginButtonAction
{
    self.isPush = NO;
    if (self.isBegin) {
        [self.scroll createTimer];
    }
    self.isBegin = NO;
}
-(void)stopButtonAction
{
    [self.scroll stopTimer];
    self.isBegin = YES;
    self.isPush = YES;
}
-(void)pushToCookingController:(GoodListModel *)model
{
    if (self.isPush) {
        CookingViewController *cookingVC = [[CookingViewController alloc] init];
        cookingVC.foodName = model.name;
        cookingVC.imagePathThumbnails = model.imagePathThumbnails;
        cookingVC.vegetable_id = model.vegetable_id;
        [self.navigationController pushViewController:cookingVC animated:YES];
    }
    
}
-(void)leftButtonAction
{
    [self.scroll stopTimer];
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}



































- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
